package com.xiaochong.bitong.job.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.bitong.job.config.hbase.HbaseBaseQueryComponent;
import com.xiaochong.bitong.job.util.StringUtil;
import com.xiaochong.tir.common.data.hbasepo.BroaderIndex;
import com.xiaochong.tir.common.data.hbasepo.IndustryIndex;
import com.xiaochong.tir.common.data.vo.ApiChartYValue;
import com.xiaochong.tir.common.data.vo.GaiNianYVo;
import com.xiaochong.tir.common.data.vo.GaiNianZhiShuVo;
import com.xiaochong.tir.common.data.vo.IndustryDataVo;
import com.xiaochong.tir.common.exception.hbase.HbaseCloseExcetion;
import com.xiaochong.tir.common.exception.hbase.HbaseConnectionExcetion;
import com.xiaochong.tir.common.exception.hbase.HbaseGetTableExcetion;
import com.xiaochong.tir.common.util.DateUtils;
import com.xiaochong.tir.common.util.HbaseTableUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/8/14.
 */
@Service
public class IndustryService {
    @Autowired
    HbaseBaseQueryComponent hbaseBaseQueryComponent;

    /**
     * 行业指数(列表)
     * @param rowKey
     * @return
     * @throws HbaseConnectionExcetion
     * @throws HbaseGetTableExcetion
     * @throws IOException
     * @throws HbaseCloseExcetion
     */
    private String getBroaderData(String rowKey) throws HbaseConnectionExcetion, HbaseGetTableExcetion, IOException, HbaseCloseExcetion {
        String grailStr = hbaseBaseQueryComponent.selectByQualifierData(HbaseTableUtils.HOME_INDEX_EVERY_DAY_RECORDING_TABLE_NAME
                , HbaseTableUtils.HOME_INDEX_EVERY_DAY_RECORDING_TABLE_FAMILY
                , HbaseTableUtils.CALCULATE_TICKER_QUALIFIER
                , rowKey
        );
        LogTrace.info("hbase行业指数查询","是否存在数据"+rowKey, StringUtils.isBlank(grailStr)?"无数据":"有数据");
        return grailStr;
    }


    /**
     * 行业指数一年的数据
     * @return
     * @throws HbaseCloseExcetion
     * @throws IOException
     * @throws HbaseGetTableExcetion
     * @throws HbaseConnectionExcetion
     */
    public IndustryDataVo getConceptualQuotesOfYear(int id) throws HbaseCloseExcetion, IOException, HbaseGetTableExcetion, HbaseConnectionExcetion {

        int day=365;
        IndustryDataVo industryDataVo = new IndustryDataVo();
        List<String> x=new ArrayList<>(  );
        List<ApiChartYValue> y=new ArrayList<>(  );
        ApiChartYValue yValue = new ApiChartYValue();

        for(int k=0;k<day;k++){
            String rowkey = id+"@"+DateUtils.getNextDayDateYMD(-k);
            String grailStr =this.getBroaderData(rowkey);
            if(StringUtils.isNotBlank(grailStr)){
                List<IndustryIndex> bil = JSON.parseArray(grailStr,IndustryIndex.class);
                if(bil.size()>0){
                    for (int i=0;i<bil.size();i++){
                        IndustryIndex bi = bil.get(i);
                        if(bi == null || bi.getCreateTime() == null || bi.getIndustryIndex()== null || bi.getIndustryRiseFall()== null || bi.getIndustryRiseFall().doubleValue()==0){
                            continue;  //内容不完整直接过滤
                        }
                        x.add(String.valueOf(( bi.getCreateTime().getTime())));
                        yValue = new ApiChartYValue();
                        yValue.setValue(bi.getIndustryIndex()!=null?bi.getIndustryIndex().setScale(2, BigDecimal.ROUND_DOWN).toString():"");
                        yValue.setContent2(bi.getIndustryRiseFall()!=null?bi.getIndustryRiseFall().multiply(BigDecimal.valueOf(100)).setScale(2,BigDecimal.ROUND_DOWN).toString():"");
                        yValue.setDifference( StringUtil.getDoubleTwo(Double.parseDouble(yValue.getValue())*Double.parseDouble(yValue.getContent2())/100)+"" );
                        y.add(yValue);
                        break;
                    }
                }
            }
        }
        Collections.reverse( x );
        Collections.reverse( y );
        industryDataVo.setX( x );
        industryDataVo.setY( y );
        return industryDataVo;
    }

    /**
     * 行业指数30天的数据
     * @return
     * @throws HbaseCloseExcetion
     * @throws IOException
     * @throws HbaseGetTableExcetion
     * @throws HbaseConnectionExcetion
     */
    public IndustryDataVo getConceptualQuotesOf30Days(int id) throws HbaseCloseExcetion, IOException, HbaseGetTableExcetion, HbaseConnectionExcetion {
        int day=30;
        int betweenHours = 4;
        IndustryDataVo industryDataVo = new IndustryDataVo();
        List<String> x=new ArrayList<>(  );
        List<ApiChartYValue> y=new ArrayList<>(  );
        ApiChartYValue yValue = new ApiChartYValue();
        List<IndustryIndex> IndustryIndexList = new ArrayList<>(  );

        for(int k=0;k<day;k++){
            String rowkey = id+"@"+DateUtils.getNextDayDateYMD(-k);
            String grailStr =this.getBroaderData(rowkey);
            if(StringUtils.isNotBlank(grailStr)){
                List<IndustryIndex> bil = JSON.parseArray(grailStr,IndustryIndex.class);
                if(bil.size()>0){
                    for(int i=bil.size()-1;i>=0;i--){
                        IndustryIndex bi = bil.get(i);
                        if(bi == null || bi.getCreateTime() == null || bi.getIndustryIndex()== null || bi.getIndustryRiseFall()== null || bi.getIndustryRiseFall().doubleValue()==0){
                            continue;  //内容不完整直接过滤
                        }
                        IndustryIndexList.add(bi);
                        i=i-47;
                    }
                }
            }
        }

        if(IndustryIndexList.size()>0){
            for(int i=0;i<IndustryIndexList.size();i++){
                IndustryIndex bi = IndustryIndexList.get( i );
                x.add(String.valueOf(bi.getCreateTime().getTime()));
                yValue = new ApiChartYValue();
                yValue.setValue(bi.getIndustryIndex()!=null?bi.getIndustryIndex().setScale(2, BigDecimal.ROUND_DOWN).toString():"");
                yValue.setContent2(bi.getIndustryRiseFall()!=null?bi.getIndustryRiseFall().multiply(BigDecimal.valueOf(100)).setScale(2,BigDecimal.ROUND_DOWN).toString():"");
                yValue.setDifference( StringUtil.getDoubleTwo(Double.parseDouble(yValue.getValue())*Double.parseDouble(yValue.getContent2())/100)+"" );
                y.add(yValue);
            }
        }

        Collections.reverse( x );
        Collections.reverse( y );
        industryDataVo.setX( x );
        industryDataVo.setY( y );
        return industryDataVo;
    }

    /**
     * 行业指数7天的数据
     * @return
     * @throws HbaseCloseExcetion
     * @throws IOException
     * @throws HbaseGetTableExcetion
     * @throws HbaseConnectionExcetion
     */
    public IndustryDataVo getConceptualQuotesOf7Days(int id) throws HbaseCloseExcetion, IOException, HbaseGetTableExcetion, HbaseConnectionExcetion {
        int day=7;
        int betweenHours = 1;
        IndustryDataVo industryDataVo = new IndustryDataVo();
        List<String> x=new ArrayList<>(  );
        List<ApiChartYValue> y=new ArrayList<>(  );
        ApiChartYValue yValue = new ApiChartYValue();
        List<IndustryIndex> IndustryIndexList = new ArrayList<>(  );

        for(int k=0;k<day;k++){
            String rowkey = id+"@"+DateUtils.getNextDayDateYMD(-k);
            String grailStr =this.getBroaderData(rowkey);
            if(StringUtils.isNotBlank(grailStr)){
                List<IndustryIndex> bil = JSON.parseArray(grailStr,IndustryIndex.class);
                if(bil.size()>0){
                    for(int i=bil.size()-1;i>=0;i--){
                        IndustryIndex bi = bil.get(i);
                        if(bi == null || bi.getCreateTime() == null || bi.getIndustryIndex()== null || bi.getIndustryRiseFall()== null||bi.getIndustryRiseFall().doubleValue()==0){
                            continue;  //内容不完整直接过滤
                        }
                        IndustryIndexList.add(bi);
                        i=i-11;
                    }
                }
            }
        }

        if(IndustryIndexList.size()>0){
            for(int i=0;i<IndustryIndexList.size();i++){
                IndustryIndex bi = IndustryIndexList.get( i );
                x.add(String.valueOf(bi.getCreateTime().getTime()));
                yValue = new ApiChartYValue();
                yValue.setValue(bi.getIndustryIndex()!=null?bi.getIndustryIndex().setScale(2, BigDecimal.ROUND_DOWN).toString():"");
                yValue.setContent2(bi.getIndustryRiseFall()!=null?bi.getIndustryRiseFall().multiply(BigDecimal.valueOf(100)).setScale(2,BigDecimal.ROUND_DOWN).toString():"");
                yValue.setDifference( StringUtil.getDoubleTwo(Double.parseDouble(yValue.getValue())*Double.parseDouble(yValue.getContent2())/100)+"" );
                y.add(yValue);
            }
        }

        Collections.reverse( x );
        Collections.reverse( y );
        industryDataVo.setX( x );
        industryDataVo.setY( y );
        return industryDataVo;
    }


    /**
     * 行业指数1天的数据(每十分钟一条)
     * @return
     * @throws HbaseCloseExcetion
     * @throws IOException
     * @throws HbaseGetTableExcetion
     * @throws HbaseConnectionExcetion
     */
    public IndustryDataVo getConceptualQuotesOfOneDays(int id) throws HbaseCloseExcetion, IOException, HbaseGetTableExcetion, HbaseConnectionExcetion {
        IndustryDataVo industryDataVo = new IndustryDataVo();
        List<String> x=new ArrayList<>(  );
        List<ApiChartYValue> y=new ArrayList<>(  );
        ApiChartYValue yValue = new ApiChartYValue();
        List<IndustryIndex> IndustryIndexList = new ArrayList<>(  );

        int betweenHours = 24;
        int nowHours = LocalDateTime.now().getHour();
        if(nowHours < betweenHours){
            String rowkey = id+"@"+DateUtils.getNextDayDateYMD(-1);
            String grailStr =this.getBroaderData(rowkey);
            if(StringUtils.isNotBlank( grailStr )){
                List<IndustryIndex> bil = JSON.parseArray(grailStr,IndustryIndex.class);
                IndustryIndexList.addAll( bil );
            }
        }
        String rowkey = id+"@"+DateUtils.getNextDayDateYMD(0);
        String grailStr =this.getBroaderData(rowkey);
            if(StringUtils.isNotBlank(grailStr)){
                List<IndustryIndex> bil = JSON.parseArray(grailStr,IndustryIndex.class);
                IndustryIndexList.addAll( bil );
                if(IndustryIndexList.size()>0){
                    for(int i=0;i<IndustryIndexList.size();i++){
                        IndustryIndex bi = IndustryIndexList.get( i );
                        if(bi != null && bi.getCreateTime() != null && bi.getIndustryIndex() != null && bi.getIndustryRiseFall() != null && bi.getIndustryRiseFall().doubleValue()!=0){
                            if(DateUtils.getDaysBetweenTwoHours(new Date(), bi.getCreateTime()) <= betweenHours){
                                    x.add(String.valueOf(bi.getCreateTime().getTime()));
                                    yValue = new ApiChartYValue();
                                    yValue.setValue(bi.getIndustryIndex()!=null?bi.getIndustryIndex().setScale(2, BigDecimal.ROUND_DOWN).toString():"");
                                    yValue.setContent2(bi.getIndustryRiseFall()!=null?bi.getIndustryRiseFall().multiply(BigDecimal.valueOf(100)).setScale(2,BigDecimal.ROUND_DOWN).toString():"");
                                    yValue.setDifference( StringUtil.getDoubleTwo(Double.parseDouble(yValue.getValue())*Double.parseDouble(yValue.getContent2())/100)+"" );
                                    y.add(yValue);
                            }
                        }
                        i++;
                    }
                }
            }

        industryDataVo.setX( x );
        industryDataVo.setY( y );
        return industryDataVo;
    }


    /**
     * 行业指数3小时的数据(每5分钟一条)
     * @return
     * @throws HbaseCloseExcetion
     * @throws IOException
     * @throws HbaseGetTableExcetion
     * @throws HbaseConnectionExcetion
     */
    public IndustryDataVo IndustryDataVo3Hours(int id) throws HbaseCloseExcetion, IOException, HbaseGetTableExcetion, HbaseConnectionExcetion {
        IndustryDataVo industryDataVo = new IndustryDataVo();
        List<String> x=new ArrayList<>(  );
        List<ApiChartYValue> y=new ArrayList<>(  );
        ApiChartYValue yValue = new ApiChartYValue();
        List<IndustryIndex> IndustryIndexList = new ArrayList<>(  );

        int betweenHours = 3;
        int nowHours = LocalDateTime.now().getHour();
        if(nowHours < betweenHours){
            String rowkey = id+"@"+DateUtils.getNextDayDateYMD(-1);
            String grailStr =this.getBroaderData(rowkey);
            if(StringUtils.isNotBlank( grailStr )){
                List<IndustryIndex> bil = JSON.parseArray(grailStr,IndustryIndex.class);
                IndustryIndexList.addAll( bil );
            }
        }
        String rowkey = id+"@"+DateUtils.getNextDayDateYMD(0);
        String grailStr =this.getBroaderData(rowkey);
        if(StringUtils.isNotBlank(grailStr)){
            List<IndustryIndex> bil = JSON.parseArray(grailStr,IndustryIndex.class);
            IndustryIndexList.addAll( bil );
            if(IndustryIndexList.size()>0){
                for(int i=0;i<IndustryIndexList.size();i++){
                    IndustryIndex bi = IndustryIndexList.get( i );
                    if(bi != null && bi.getCreateTime() != null && bi.getIndustryIndex() != null && bi.getIndustryRiseFall() != null && bi.getIndustryRiseFall().doubleValue()!=0){
                        if(DateUtils.getDaysBetweenTwoHours(new Date(), bi.getCreateTime()) <= betweenHours){
                            x.add(String.valueOf(bi.getCreateTime().getTime()));
                            yValue = new ApiChartYValue();
                            yValue.setValue(bi.getIndustryIndex()!=null?bi.getIndustryIndex().setScale(2, BigDecimal.ROUND_DOWN).toString():"");
                            yValue.setContent2(bi.getIndustryRiseFall()!=null?bi.getIndustryRiseFall().multiply(BigDecimal.valueOf(100)).setScale(2,BigDecimal.ROUND_DOWN).toString():"");
                            yValue.setDifference( StringUtil.getDoubleTwo(Double.parseDouble(yValue.getValue())*Double.parseDouble(yValue.getContent2())/100)+"" );
                            y.add(yValue);
                        }
                    }
                }
            }
        }

        industryDataVo.setX( x );
        industryDataVo.setY( y );
        return industryDataVo;
    }
}
